﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ARS.Control.Attributes;
using ARS.Control.Custom.DataPaging.Attributes;
using ARS.DataBase.SqlServerProvider;
using MySql.Data.MySqlClient;
using DbSelect = ARS.Control.Custom.DataPaging.Attributes.DbSelect;

//using DbSelect = ARS.Control.DataPaging.Attributes.DbSelect;

namespace ARS.DataBase.MySqlProvider
{
    public class DataPagingProvider : ARS.DataBase.Provider.IDataPagingProvider
    {
        //public override DataSet DataSet(ARS.Control.Attributes.DbSelectAll dsa)
        //{

        //}
        //public override DataSet GetDataList(DbSelect selectMode, bool isStatistics = true)
        //{
        //    throw new NotImplementedException();
        //}
        public override DataSet GetDataList(DbSelectAll selectModel)
        {

            return DbHelperMySQL.Query(selectModel.GetSelectAllSql());
        }

        public override DataSet GetDataList(DbSelect selectModel, bool isStatistics = true)
        {
            MySqlParameter[] parameters =
            {
                new MySqlParameter("@TableName", MySqlDbType.VarChar, 4000), //@tablename varchar(100),--表名或视图表 
                new MySqlParameter("@SelectFields", MySqlDbType.VarChar, 4000), //@fieldlist varchar(4000)='*',--欲选择字段列表 
                new MySqlParameter("@OrderField", MySqlDbType.VarChar, 100), //@orderfield varchar(100),--排序字段 
                new MySqlParameter("@KeyField", MySqlDbType.VarChar, 100), //@keyfield varchar(100),--主键 
                new MySqlParameter("@PageIndex", MySqlDbType.Int32), //@pageindex int,--页号,从0开始 
                new MySqlParameter("@PageSize", MySqlDbType.Int32), //@pagesize int=20,--页尺寸
                new MySqlParameter("@StrWhere", MySqlDbType.VarChar, 4000), //@strwhere varchar(4000),--条件 
                new MySqlParameter("@OrderType", MySqlDbType.Bit), //@ordertype bit=1--排序,1,降序,0,升序
                new MySqlParameter("@ResultCount", MySqlDbType.Int32), //@pagesize int=20,--页尺寸
                new MySqlParameter("@ResultPages", MySqlDbType.Int32), //@pagesize int=20,--页尺寸
            };
            parameters[0].Value = selectModel.TableName;
            parameters[1].Value = selectModel.SelectFields;
            parameters[2].Value = selectModel.OrderField;
            parameters[3].Value = selectModel.KeyField;
            parameters[4].Value = selectModel.PageIndex;
            parameters[5].Value = selectModel.PageSize;
            parameters[6].Value = selectModel.StrWhere;
            parameters[7].Value = selectModel.OrderType;
            parameters[8].Direction = ParameterDirection.Output;
            parameters[9].Direction = ParameterDirection.Output;
            if (isStatistics)
            {
                int RecordCount = 0;
                int totalPage = 0;
                using (MySqlConnection connection = new MySqlConnection(DbHelperMySQL.connectionString))
                {
                    DataSet dataSet = new DataSet();
                    connection.Open();
                    MySqlDataAdapter sqlDA = new MySqlDataAdapter();
                    sqlDA.SelectCommand = DbHelperMySQL.BuildQueryCommand(connection, base.GetDataPaging_List_ProcName(), parameters);
                    sqlDA.Fill(dataSet, "ds");

                    selectModel.ResultCount = Convert.ToInt32(sqlDA.SelectCommand.Parameters[8].Value);
                    //selectModel.ResultPages = Convert.ToInt32(sqlDA.SelectCommand.Parameters[9].Value);
                    selectModel.ResultPages = selectModel.ResultCount/selectModel.PageSize + 1;

                    connection.Close();
                    return dataSet;
                }
            }
            else
            {
                return DbHelperMySQL.RunProcedure(base.GetDataPaging_List_ProcName(), parameters, "ds");

            }

        }


        public override DataPagingDb GetDataPagingDbModel(DataPagingQueryModel queryModel)
        {
            string strSql = string.Format("select * from {0} where Status_UserId=@Status_UserId and Status_ClassName=@Status_ClassName", GetDbTableName());
            MySqlParameter[] parameters = {
					new MySqlParameter("@Status_UserId", MySqlDbType.Int32,4),
                    new MySqlParameter("@Status_ClassName", MySqlDbType.VarChar,50)
			};
            parameters[0].Value = queryModel.UserId;
            parameters[1].Value = queryModel.ClassName;
            DataSet ds = DbHelperMySQL.Query(strSql, parameters);
            return base.GetDataPagingDbModel(ds);
        }

        public override DataPagingDb GetDataPagingDbModel(string name, string value)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("select   Id,Status_ClassName,DbSelect_TableName,DbSelect_SelectFields,DbSelect_OrderField,DbSelect_OrderType,DbSelect_PageIndex,DbSelect_PageSize,DbSelect_StrWhere,DbSelect_Key,HtmlTable_TableCssName,HtmlTable_TBodyTrHtml,HtmlTable_THeadTrHtml,Operat_OperatHtml,Operat_IsHasOperat,Operat_ColumnName,DbOperat_DeleteSql,Status_UserId,HtmlFanye_FanyeHtml,HtmlFanye_IsHasFanye,Where_KeyValue,DbSelect_SearchWhere,DbOperat_OperatSqlList from DataPaging ");
            strSql.Append(string.Format(base.GetSelectSql(),name));
            MySqlParameter[] parameters = {
					new MySqlParameter("@"+name, MySqlDbType.Int32,4)
			};
            parameters[0].Value = value;

            
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            return base.GetDataPagingDbModel(ds);
        }



        public override DataPagingDb GetDataPagingDbModel(int id)
        {
            return GetDataPagingDbModel("Id", id.ToString());
        }

        public override int DataPagingDbAdd(DataPagingDb model)
        {
            int rowsAffected = 0;
            MySqlParameter[] parameters = {
					new MySqlParameter("proc_Id", MySqlDbType.Int32,11),
					new MySqlParameter("proc_Status_ClassName", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbSelect_TableName", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbSelect_SelectFields", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbSelect_OrderField", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbSelect_PageIndex", MySqlDbType.Int32,11),
					new MySqlParameter("proc_DbSelect_PageSize", MySqlDbType.Int32,11),
					new MySqlParameter("proc_DbSelect_StrWhere", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbSelect_KeyField", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_HtmlTable_TableCssName", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_HtmlTable_TBodyTrHtml", MySqlDbType.VarChar,1000),
					new MySqlParameter("proc_Operat_OperatHtml", MySqlDbType.VarChar,1000),
					new MySqlParameter("proc_Operat_IsHasOperat", MySqlDbType.Bit),
					new MySqlParameter("proc_Operat_ColumnName", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbOperat_DeleteSql", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_Status_UserId", MySqlDbType.Int32,11),
					new MySqlParameter("proc_HtmlFanye_FanyeHtml", MySqlDbType.VarChar,1000),
					new MySqlParameter("proc_HtmlFanye_IsHasFanye", MySqlDbType.Bit),
					new MySqlParameter("proc_DbSelect_OrderType", MySqlDbType.Int16,4),
					new MySqlParameter("proc_HtmlTable_THeadTrHtml", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbOperat_OperatSqlList", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_HtmlTable_TableTrCssName", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbOperat_OrderList", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbOperat_SearchList", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_DbSelect_UniteWhere", MySqlDbType.VarChar,255),
					new MySqlParameter("proc_Operat_TagString", MySqlDbType.VarChar,50),
					new MySqlParameter("proc_Status_ControlId", MySqlDbType.VarChar,50)                                          };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Status.ClassName;
            parameters[2].Value = model.DbSelect.TableName;
            parameters[3].Value = model.DbSelect.SelectFields;
            parameters[4].Value = model.DbSelect.OrderField;
            parameters[5].Value = model.DbSelect.PageIndex;
            parameters[6].Value = model.DbSelect.PageSize;
            parameters[7].Value = model.DbSelect.StrWhere;
            parameters[8].Value = model.DbSelect.KeyField;
            parameters[9].Value = model.HtmlTable.TableCssName;
            parameters[10].Value = model.HtmlTable.TBodyTrHtml;
            parameters[11].Value = model.Operat.OperatHtml;
            parameters[12].Value = model.Operat.IsHasOperat;
            parameters[13].Value = model.Operat.ColumnName;
            parameters[14].Value = model.DbOperat.DeleteSql;
            parameters[15].Value = model.Status.UserId;
            parameters[16].Value = model.HtmlFanye.FanyeHtml;
            parameters[17].Value = model.HtmlFanye.IsHasFanye;
            parameters[18].Value = model.DbSelect.OrderType;
            parameters[19].Value = model.HtmlTable.THeadTrHtml;
            parameters[20].Value = model.DbOperat.OperatSqlList;
            parameters[21].Value = model.HtmlTable.TableTrCssName;
            parameters[22].Value = model.DbOperat.OrderList;
            parameters[23].Value = model.DbOperat.SearchList;
            parameters[24].Value = model.DbSelect.UniteWhere;
            parameters[25].Value = model.Operat.TagString;
            parameters[26].Value = model.Status.ControlId;
            DbHelperMySQL.RunProcedure(base.GetDataPaging_Model_ProcName(), parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return rowsAffected;
            }
            else
            {
                return rowsAffected;
            }

          

        }



//        public override DataPagingDb GetDataPagingDbModel(DataPagingQueryModel queryModel)
//        {
//            throw new NotImplementedException();
//        }
    }
}
